package com.znz.domain;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import javax.persistence.*;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: johnson
 * Date: 5/7/15
 * Time: 4:48 下午
 */

@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", identifierColumn = "id", identifierField = "id", table = "t_return_visit")
public class Treturnvisit {

    //
    @ManyToOne
    @JoinColumn(name = "order_id")
	private Torder torder;
	//	订单号
    @Column(name="order_no")
	private String orderNo;
	//	回访时间
    @Column(name="return_visit_date")
	private Date returnVisitDate;
	//	回访类型 0 普通回访 1 接单回访 2 结单回访
    @Column(name="type")
	private String type;
	//	客服ID
    @ManyToOne
    @JoinColumn(name="c_user_id")
	private Tcustomer cUserId;
	//	回访前的备注
    @Column(name="remark")
	private String remark;
	//	是否回访0否1是
    @Column(name="is_return_visit")
	private String isReturnVisit;
	//	回访后的记录
    @Column(name="return_visit_record")
	private String returnVisitRecord;
	//	创建时间
    @Column(name="create_date")
	private Date createDate;
	//	修改时间
    @Column(name="update_date")
	private Date updateDate;


    public static List<Treturnvisit> getReturnVisitByWhere(String where,List<Object> params,int firstResult,int maxResults){
        StringBuilder str_Quert=new StringBuilder("SELECT o FROM Treturnvisit o   where o.id!=null  ");
        str_Quert.append(where);
        str_Quert.append(" order by o.id ");
        TypedQuery<Treturnvisit> query=entityManager().createQuery(str_Quert.toString(),Treturnvisit.class).setFirstResult(firstResult).setMaxResults(maxResults);
            int i=1;
            for (Object key:params){
                query.setParameter(i,key);
                i++;
            }
            return query.getResultList();
        }

    /**
     *方法说明:根据订单ID获取所有医院列表
     * @param orderId  订单ID
     * @return
     */
    public static List<Treturnvisit> getReturnVisitListByOrderId(Long orderId){
       StringBuilder sbsql=new StringBuilder();
       sbsql.append("select tohp.* from t_order_hospital_project tohp ");
       sbsql.append("join  t_order torder on toder.id=tohp.order_id");
       sbsql.append("where toder.id=? and enabled!=1 ");
       Query query=entityManager().createNativeQuery(sbsql.toString(),TorderHospitalProject.class);
       query.setParameter(1,orderId);
       return query.getResultList();
    }

    /**
     * 方法说明:根据当前客服id获取当天待回访的数量
     * @param userId  客服IDs
     * @return
     */
    public static Long getReturnVisitCount(String userId){
        StringBuilder sbsql=new StringBuilder();
        sbsql.append("select count(1) from t_customer tc ");
        sbsql.append("join t_order_hospital_project tohp on tc.id=tohp.c_user_id ");
        sbsql.append("join t_order torder on tohp.order_id=torder.id  ");
        sbsql.append("where tc.id=? and torder.return_visit_success=0 and torder.enabled=0 ");
        Query query=entityManager().createNativeQuery(sbsql.toString());
        query.setParameter(1,userId);
        Object obj=query.getSingleResult();
        return Long.parseLong(obj.toString());
    }
    public static double getAchievement(String where,List<Object> params){
        StringBuilder strSql=new StringBuilder();
        strSql.append(" select count(t2.cost) from t_order t1 ");
        strSql.append(" join t_order_hospital_project t2 on t1.id=t2.order_id");
        strSql.append(" join t_customer t3 on t2.h_user_id=t3.id");
        strSql.append(" where t1.state=4 ");
        strSql.append(where);
        Query  query=entityManager().createNativeQuery(strSql.toString());
        if(null!=params && params.size()>0){
            int i=1;
            for (Object key:params){
                query.setParameter(1,key);
            }
          Object achievement=query.getSingleResult();
          if(null!=achievement){
             return  Double.parseDouble(achievement.toString());
          }
            return 0;
        }
         return 0;
    }
}
